package com.gopay.scheduler.service.cbrmb.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.scheduler.service.cbrmb.CbRmbAutoSettleService;


@Service(value = "cbRmbAutoSettleService")
public class CbRmbAutoSettleServiceImpl implements CbRmbAutoSettleService {

	@Resource(name = "commonQueryDAO")
    private CommonQueryDAO commonQueryDAO;
	/**
	 * 查询订单之零点判断
	 * @param startDay
	 * @param today
	 * @return
	 */
	public List<CpsGenMainOrder> queryOrderOfJudge(String startDay,String today){
		List<CpsGenMainOrder> orderList=new ArrayList<CpsGenMainOrder>();
        
        StringBuilder sb = new StringBuilder();
      
        sb.append("  select m.gopay_order_id, ");
        sb.append("  m.gopay_int_txn_cd, ");
        sb.append("  p.CUST_AUDIT_TIME, ");
        sb.append("  m.pay_cust_id, ");
        sb.append("  m.pay_acct, ");
        sb.append("  p.settlement_cycle, ");
        sb.append("  p.recv_bank_acct_num, ");
        sb.append("  p.RECV_BANK_NAME, ");
        sb.append("  m.MER_ID, ");
        sb.append("  p.BC_FLAG, ");
        sb.append("  m.MER_TXN_AMT ");
        sb.append("  from CPS_GEN_MAIN_ORDER m ");
        sb.append("  left join CPS_GEN_PAY_ORDER p on p.gopay_order_id=m.gopay_order_id ");
        sb.append("  left join wf_appr_task_dtl td on (td.target_id=m.gopay_order_id and td.approval_id=3) ");
        sb.append("  left join wf_appr_decision deci on deci.task_id=td.task_id ");
        sb.append("  where 1=1 ");
        sb.append("  and deci.decision_sq is null ");
        sb.append("  and p.auto_settle is null ");
        sb.append("  and p.settlement_cycle>0 ");
        sb.append("  and m.txn_sta_cd=30000 ");
        sb.append("  and m.gopay_int_txn_cd ='01721' ");
        sb.append("  and p.CUST_AUDIT_TIME<to_timestamp(:dateEnd, 'yyyymmddHH24miss') ");
        sb.append("  and p.CUST_AUDIT_TIME>=to_timestamp(:dateBegin, 'yyyymmddHH24miss') ");
        sb.append(" order by m.gopay_txn_tm asc ");
        
               
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("dateBegin",startDay+"000000");
        map.put("dateEnd",today+"000000");
       
        
        
        List<Object[]> rsList = commonQueryDAO.findBySql(sb.toString(), map);
        CpsGenMainOrder main;
        
        if (rsList != null && rsList.size() > 0) {
            for (Object[] arr : rsList) {
                main=new CpsGenMainOrder();
                main.setGopayOrderId((String) arr[0]);
                main.setGopayIntTxnCd((String) arr[1]);
                main.setGopayTxnTm((Date) arr[2]);
                main.setPayCustId((String) arr[3]);
                main.setPayAcct((String) arr[4]);
                
                int settleCircle=arr[5]!=null?((BigDecimal)arr[5]).intValue():0;                
                String cardNum=(String) arr[6];
                String bankName=(String) arr[7];
                
                main.setMerId((String) arr[8]);
                
                main.setTransDtlSq(settleCircle);
                main.setRemark1(cardNum);
                main.setRemark2(bankName);
                main.setOrderType(((BigDecimal) arr[9]).toString());
                logger.error("====>"+((BigDecimal) arr[9]).toString());
                main.setMerTxnAmt((BigDecimal)arr[10]);
                
                orderList.add(main);
            }
        }
        
        return orderList;
	}
	protected Logger logger = Logger.getLogger(getClass());
	
	
	/**
	 * 查询订单之十点银企直连
	 * @param startDay
	 * @param today
	 * @return
	 */
	public List<CpsGenMainOrder> queryOrderOfBCDL(String startDay,String today){
		List<CpsGenMainOrder> orderList=new ArrayList<CpsGenMainOrder>();
        
        StringBuilder sb = new StringBuilder();
      
        sb.append("  select m.gopay_order_id, ");
        sb.append("  m.gopay_int_txn_cd ");
        sb.append("  from CPS_GEN_MAIN_ORDER m ");
        sb.append("  left join CPS_GEN_PAY_ORDER p on p.gopay_order_id=m.gopay_order_id ");
        sb.append("  left join wf_appr_task_dtl td on (td.target_id=m.gopay_order_id and td.approval_id=3) ");
        sb.append("  left join wf_appr_decision deci on deci.task_id=td.task_id ");
        sb.append("  where 1=1 ");
        sb.append("  and deci.decision_sq is null ");
        sb.append("  and p.rule_id is not null ");
        sb.append("  and p.PAY_STAT ='01' ");
        sb.append("  and p.auto_settle ='1' ");
        sb.append("  and p.settlement_cycle>0 ");
        //sb.append("  and m.ACCOUNTING_POINT='1' ");
        sb.append("  and m.txn_sta_cd=30000 ");
        sb.append("  and m.gopay_int_txn_cd='01721' ");
        sb.append("  and p.CUST_AUDIT_TIME<to_timestamp(:dateEnd, 'yyyymmddHH24miss') ");
        sb.append("  and p.CUST_AUDIT_TIME>=to_timestamp(:dateBegin, 'yyyymmddHH24miss') ");
        sb.append(" order by m.gopay_txn_tm asc ");
        
               
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("dateBegin",startDay+"000000");
        map.put("dateEnd",today+"000000");
       
        
        
        List<Object[]> rsList = commonQueryDAO.findBySql(sb.toString(), map);
        CpsGenMainOrder main;
        
        if (rsList != null && rsList.size() > 0) {
            for (Object[] arr : rsList) {
                main=new CpsGenMainOrder();
                main.setGopayOrderId((String) arr[0]);
                main.setGopayIntTxnCd((String) arr[1]);
                
                orderList.add(main);
            }
        }
        
        return orderList;
	}


	@Override
	public List<CpsGenMainOrder> queryOrderOfCbRmbJw(String startDay,
			String today) {
		// TODO Auto-generated method stub
	List<CpsGenMainOrder> orderList=new ArrayList<CpsGenMainOrder>();
        
        StringBuilder sb = new StringBuilder();
      
        sb.append("  select m.gopay_order_id, ");
        sb.append("  m.gopay_int_txn_cd, ");
        sb.append("  p.CUST_AUDIT_TIME, ");
        sb.append("  m.pay_cust_id, ");
        sb.append("  m.pay_acct, ");
        sb.append("  p.settlement_cycle, ");
        sb.append("  p.recv_bank_acct_num, ");
        sb.append("  p.RECV_BANK_NAME, ");
        sb.append("  m.MER_ID, ");
        sb.append("  p.BC_FLAG, ");
        sb.append("  m.MER_TXN_AMT ");
        sb.append("  from CPS_GEN_MAIN_ORDER m ");
        sb.append("  left join CPS_GEN_PAY_ORDER p on p.gopay_order_id=m.gopay_order_id ");
        sb.append("  left join wf_appr_task_dtl td on (td.target_id=m.gopay_order_id and td.approval_id=19) ");
        sb.append("  left join wf_appr_decision deci on deci.task_id=td.task_id ");
        sb.append("  left join cb_rmb_tran_assist_dtl rmb on m.gopay_order_id=rmb.gopay_order_id ");
        sb.append("  where 1=1 ");
        sb.append("  and deci.decision='1' ");//查询审批通过的
       // sb.append("  and p.auto_settle is null ");
        sb.append("  and p.settlement_cycle>0 ");
        sb.append("  and rmb.state='00' ");//初始化的才行
        sb.append("  and m.txn_sta_cd=30000 ");
        sb.append("  and m.gopay_int_txn_cd ='01726' ");
        sb.append("  and p.CUST_AUDIT_TIME<=to_timestamp(:dateEnd, 'yyyymmddHH24miss') ");//这个是临时修改，必须是小于，不查今天的订单
        sb.append("  and p.CUST_AUDIT_TIME>=to_timestamp(:dateBegin, 'yyyymmddHH24miss') ");
        sb.append(" order by m.gopay_txn_tm asc ");
        
               
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("dateBegin",startDay+"000000");
        map.put("dateEnd",today+"000000");//本来应该是000000，回头要改回来的
       
        
        
        List<Object[]> rsList = commonQueryDAO.findBySql(sb.toString(), map);
        CpsGenMainOrder main;
        
        if (rsList != null && rsList.size() > 0) {
            for (Object[] arr : rsList) {
                main=new CpsGenMainOrder();
                main.setGopayOrderId((String) arr[0]);
                main.setGopayIntTxnCd((String) arr[1]);
                main.setGopayTxnTm((Date) arr[2]);
                main.setPayCustId((String) arr[3]);
                main.setPayAcct((String) arr[4]);
                
                int settleCircle=arr[5]!=null?((BigDecimal)arr[5]).intValue():0;                
                String cardNum=(String) arr[6];
                String bankName=(String) arr[7];
                
                main.setMerId((String) arr[8]);
                
                main.setTransDtlSq(settleCircle);
                main.setRemark1(cardNum);
                main.setRemark2(bankName);
                main.setOrderType(((BigDecimal) arr[9]).toString());
                logger.error("====>"+((BigDecimal) arr[9]).toString());
                main.setMerTxnAmt((BigDecimal)arr[10]);
                
                orderList.add(main);
            }
        }
        
        return orderList;
	}
}
